package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    MenuService menuService;

    @RequestMapping("/findMenuByUserName")
    public Result findMenuByUserName() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = user.getUsername();
        List<Menu> menus = menuService.findMenuByUserName(userName);
        List<Map> results = new ArrayList<>();
        for (Menu menu : menus) {
            Map map = new HashMap();
            map.put("path",menu.getPath());
            map.put("title",menu.getName());
            map.put("icon",menu.getIcon());
            List<Menu> children = menu.getChildren();
            map.put("children",children);
            results.add(map);
        }
        return Result.success("",results);
    }
}
